//
//  sortHelper.swift
//  sorting
//
//  Created by lhc on 2023/8/17.
//

import Foundation
enum SortName: String {
    case 选择排序
    case 插入排序
    case 归并排序
    case 自底向上归并排序
    case 快速排序
    case 三路快速排序
    case 冒泡排序
    case 希尔排序
}


class SortHelper {
    static func isSort<T: Comparable>(sequence: [T]) -> Bool {
        for i in 1..<sequence.count {
            if sequence[i-1] > sequence[i] {
                return false
            }
        }
        return true
    }
    
    static func sortTest<T: Comparable>(sortName: SortName, sequence: [T]) {
        var list = sequence
        
        let startTime = Date().timeIntervalSince1970
        switch sortName {
        case .选择排序:
            list = SelectSort.sort(l: list)
        case .插入排序:
            list = InsertSort.sort(l: list)
        case .归并排序:
            MergeSort.sort(l: &list)
        case .自底向上归并排序:
            MergeSort.sortFromBelow(l: &list)
        case .快速排序:
            QuickSort.sort(arr: &list)
        case .三路快速排序:
            QuickSort.sortThreeWays(arr: &list)
        case .冒泡排序:
            BubbleSort.sort(arr: &list)
        case .希尔排序:
            ShellSort.sort(arr: &list)
        
        }
        let endTime = Date().timeIntervalSince1970

        if isSort(sequence: list) {
            print("\(sortName) n = \(sequence.count) 消耗 \(TimeInterval(endTime - startTime).description) 秒")
        }else {
            print("排序失败")
        }
        
        
    }
}
